package com.bootdo.wx.controller;

import com.bootdo.wx.domain.GoodsProductDO;
import com.bootdo.wx.service.GoodsProductService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品产品
 * 
 * @author sys
 * @email 1992lcg@163.com
 * @date 2019-03-19 16:39:22
 */
 
@Controller
@RequestMapping("/wx/goodsProduct")
public class GoodsProductController {
	@Autowired
	private GoodsProductService goodsProductService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsProduct:goodsProduct")
	String GoodsProduct(){
	    return "wx/goodsProduct/goodsProduct";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsProduct:goodsProduct")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GoodsProductDO> goodsProductList = goodsProductService.list(query);
		int total = goodsProductService.count(query);
		PageUtils pageUtils = new PageUtils(goodsProductList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsProduct:add")
	String add(){
	    return "wx/goodsProduct/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsProduct:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsProductDO goodsProduct = goodsProductService.get(id);
		model.addAttribute("goodsProduct", goodsProduct);
	    return "wx/goodsProduct/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsProduct:add")
	public R save( GoodsProductDO goodsProduct){
		if(goodsProductService.save(goodsProduct)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsProduct:edit")
	public R update( GoodsProductDO goodsProduct){
		goodsProductService.update(goodsProduct);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsProduct:remove")
	public R remove( String id){
		GoodsProductDO goodsProductDO = goodsProductService.get(id);
		goodsProductDO.setDelFlag(1);
		goodsProductDO.setGmtModified(new Date());
		if(goodsProductService.update(goodsProductDO)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsProduct:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		for (String id : ids) {
			GoodsProductDO goodsProductDO = goodsProductService.get(id);
			goodsProductDO.setDelFlag(1);
			goodsProductDO.setGmtModified(new Date());
			goodsProductService.update(goodsProductDO);
		}
		return R.ok();
	}

	/**
	 * 根据goodsId查询规格列表
	 */
	@RequestMapping("/getListByGoodsId")
	@ResponseBody
	public R getListByGoodsId(@RequestBody Map<String,Object> map){
		List<GoodsProductDO> list = goodsProductService.list(map);
		return R.ok().put("data",list);
	}
	
}
